﻿
namespace Sai.Core.Introspection
{
    using System;

    public class Namespace : IComparable<Namespace>
    {
        public static readonly Namespace GlobalNamespace = new Namespace();
        public static readonly Namespace SystemNamespace = new Namespace("System");

        public Namespace()
        {
            this.Name = string.Empty;
        }

        public Namespace(string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get;
            private set;
        }

        public static bool operator ==(Namespace a, Namespace b)
        {
            if (object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object)a == null) || ((object)b == null))
            {
                return false;
            }

            return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase) == 0;
        }

        public static bool operator !=(Namespace a, Namespace b)
        {
            return !(a == b);
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            Namespace another = obj as Namespace;

            if ((object)another == null)
            {
                return false;
            }

            return string.Compare(this.Name, another.Name, StringComparison.OrdinalIgnoreCase) == 0;
        }

        public override string ToString()
        {
            return this.Name;
        }

        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }

        public int CompareTo(Namespace other)
        {
            if (other == null)
            {
                return 1;
            }

            return this.Name.CompareTo(other.Name);
        }

        public static FullyQualifiedName operator +(Namespace namespaceName, Name typeName)
        {
            if (namespaceName == null)
                namespaceName = new Namespace();

            return new FullyQualifiedName(namespaceName, typeName);
        }
    }
}
